package com.gibs.peixunapi.service.impl;

import com.gibs.peixunapi.VO.SubOptionVO;
import com.gibs.peixunapi.VO.SubjectHubVO;
import com.gibs.peixunapi.dao.CourseSubjectHubDao;
import com.gibs.peixunapi.enums.ResultEnum;
import com.gibs.peixunapi.mapper.CourseSubjectHubMapper;
import com.gibs.peixunapi.model.CourseSubjectHub;
import com.gibs.peixunapi.model.SubjectHub;
import com.gibs.peixunapi.result.Result;
import com.gibs.peixunapi.service.CourseSubjectHubService;
import com.gibs.peixunapi.service.SubOptionService;
import com.gibs.peixunapi.utils.ConverterUtil;
import com.gibs.peixunapi.utils.ResultUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.persistence.EntityNotFoundException;
import javax.validation.Valid;
import java.util.ArrayList;
import java.util.List;

@Service
@Slf4j
public class CourseSubjectHubServiceImpl implements CourseSubjectHubService{

	@Autowired
	private CourseSubjectHubDao courseSubjectHubDao;
	@Autowired
	private SubOptionService subOptionService;
	@Autowired
	private CourseSubjectHubMapper courseSubjectHubMapper;

	@Transactional
@Override
	public Result create(@Valid CourseSubjectHub object) {
		try {
		
			courseSubjectHubDao.save(object);
			return ResultUtil.success(ResultEnum.CREATE_SUCCESS);
		} catch (Exception e) {
			log.error(e.getMessage());
			return ResultUtil.error(ResultEnum.SERVER_ERROR);
		}
	}

	@Transactional
@Override
	public Result save(@Valid CourseSubjectHub object) {
		try {
//
//			CourseSubjectHub courseSubjectHub = courseSubjectHubDao.findOne(object.getId());
//			ConverterUtil.copyNotNullProperties(object,courseSubjectHub);
//			courseSubjectHubDao.save(courseSubjectHub);
			return ResultUtil.success(ResultEnum.UPDATE_SUCCESS);
		} catch (Exception e) {
			log.error(e.getMessage());
			return ResultUtil.error(ResultEnum.SERVER_ERROR);
		}
	}

@Override
	public Result delete(Integer id) {
		try {
//			String[] ids = id.split(",");
//			for (String str : ids) {
//				courseSubjectHubDao.delete(Integer.valueOf(str));
//			}
			return ResultUtil.success(ResultEnum.DELETE_SUCCESS);
		} catch (Exception e) {
			log.error(e.getMessage());
			return ResultUtil.error(ResultEnum.SERVER_ERROR);
		}
	}



	@Override//TODO waiting to code
	public Result linkSubjectHubByCourseId(Integer courseId) {
		return null;
	}

	@Override
	public Result getSubjectList(Integer courseId, String type) {
		List<SubjectHub> subjectHubList=courseSubjectHubMapper.findSubjectListByCourseId(courseId, null);
		 if (subjectHubList.isEmpty()){
			 throw new EntityNotFoundException();
		 }
		List<SubjectHubVO> subjectHubVOList = new ArrayList<>(subjectHubList.size());
		for (SubjectHub subjectHub : subjectHubList) {
			SubjectHubVO temp = new SubjectHubVO();
			ConverterUtil.copyProperties(subjectHub,temp,"answer","analysis","knowPoint");
			List<SubOptionVO> subOptionVOList = subOptionService.getSubOptions(subjectHub.getId());
			if (!subjectHubList.isEmpty()){
				temp.setOptionVOList(subOptionVOList);
			}
			subjectHubVOList.add(temp);
		}
		return ResultUtil.success(ResultEnum.FIND_SUCCESS,subjectHubVOList);
	}


}

